package com.kaikeba.controller;

import com.github.pagehelper.PageInfo;
import com.kaikeba.bean.Admin;
import com.kaikeba.bean.Student;
import com.kaikeba.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletResponse;

@Controller
public class AdminController {

    @Autowired
    @Qualifier(value = "adminServiceImpl")
    private  AdminService adminService;

    @RequestMapping("/login")
    public String login(Admin admin, HttpServletResponse resp){
        String account = admin.getAccount();
        String password = admin.getPassword();
        System.out.println("登录---账号："+account+",密码："+password);

        if (account!=null&&account!=""&&password!=null&&password!=""){
            Admin user = adminService.selectByAccountAndPassword(admin);
            if (user!=null){
             return "redirect:/mainpage";
            }else {
              /*  try {
                    resp.getWriter().write("<script>alert('账号或密码错误')</script>");
                } catch (IOException e) {
                    e.printStackTrace();
                }*/
                return "login";
            }
        }else {

            return "login";
        }

    }

   @RequestMapping("/mainpage")
    public String mainPage(Model model,@RequestParam(defaultValue = "1") int index){
        PageInfo<Student>  pageInfo = adminService.findAllStu(index,-1,null,null);
        model.addAttribute("pageinfo",pageInfo);
           model.addAttribute("gradeid",null);
           model.addAttribute("studentno", null);
           model.addAttribute("stuname",null);
        return "main";
    }

    @RequestMapping("/find")
    public String find(Student stu,Model model,@RequestParam(defaultValue = "1") int index){
        Integer gradeid = stu.getGradeid();
        String studentno = stu.getStudentno();
        String stuname = stu.getStuname();
        System.out.println(gradeid+studentno+stuname);
        PageInfo<Student> allStu = adminService.findAllStu(index, gradeid, studentno, stuname);
        model.addAttribute("pageinfo",allStu);
        model.addAttribute("gradeid",gradeid);
        model.addAttribute("studentno",studentno);
        model.addAttribute("stuname",stuname);
        return "main";
    }
}
